<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>

<style>
.control{
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
  background-color: black;
}
.control i,
.control b{
  float: left;
  width: 60px;
  height: 60px;
  line-height: 60px;
  font-style: normal;
  font-weight: normal;
  text-decoration: cadetblue;
  color: white;
  font-size: 36px;
  text-align: center;
}
.control b.red{
  background-color: red;
}
.control b.green{
  background-color: green;
}
.control b.yellow{
  background-color: yellow;
}
</style>

<span class="control">
  <i id="count">00</i>
  <b id="light" class="red"></b>
</span>

<script>
var lights = [
  { light: 'red', time: 20},
  { light: 'yellow', time: 10},
  { light: 'green', time: 30},
]

//顺序按 红灯 -> 黄灯 -> 绿灯
start( lights );

function start( lights, i=0 ){

  if(i >= lights.length) i = 0;
  var time = lights[i].time;

  var int = setInterval(function(){

    //倒计时
    time = time - 1;
    
    //设置倒数计时和灯的颜色
    count.innerText = ('0' + time).slice(-2);
    light.className = lights[i].light;

    //小于等于0 说明该灯计时结束
    if(time <= 0){
      clearInterval( int );
      start( lights, ++i );
    }
  }, 1000);
}

</script>
  
</body>
</html>